﻿
/*********************************************************************************
--时间：2012-12-17 09:48:32
--作者：jjpeng
--邮箱：ling121211@gmail.com
--说明：Mongodb查询过滤器
 * 
 * 基于官方驱动 V2.2.1版本
 * 
 * Query 类中的 QueryBuilder 的方法返回类型 QueryDocument 继承自 IMongoQuery 和 BsonDocument
--
*********************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;

namespace PCC.Framework.Mongodb
{
    public class MongoDBFilter
    {
        private IMongoQuery _doc;

        public IMongoQuery Document
        {
            get
            {
                return _doc;
            }
        }

        private MongoDBFilter(IMongoQuery bsonDoc)
        {
            _doc = bsonDoc;
        }

        public static MongoDBFilter Null()
        {
            return new MongoDBFilter(Query.Null);
        }

        public static MongoDBFilter Eq(string name, object value)
        {
            BsonValue bvalue = (BsonValue)value;
            return new MongoDBFilter(Query.EQ(name, bvalue));
        }

        public static MongoDBFilter In(string name, Array values)
        {
            BsonArray bsonArr = new BsonArray(values);
            return new MongoDBFilter(Query.In(name, bsonArr));
        }

        public static MongoDBFilter NotIn(string name, Array values)
        {
            return new MongoDBFilter(Query.NotIn(name, new BsonArray(values)));
        }

        public static MongoDBFilter Gt(string name, object value)
        {
            BsonValue bValue = (BsonValue)value;
            return new MongoDBFilter(Query.GT(name, bValue));
        }

        public static MongoDBFilter Gte(string name, object value)
        {
            return new MongoDBFilter(Query.GTE(name, (BsonValue)value));
        }

        public static MongoDBFilter Lt(string name, object value)
        {
            return new MongoDBFilter(Query.LT(name, (BsonValue)value));
        }

        public static MongoDBFilter Lte(string name, object value)
        {
            return new MongoDBFilter(Query.LTE(name, (BsonValue)value));
        }

        public static MongoDBFilter And(params MongoDBFilter[] filters)
        {
            List<IMongoQuery> querys = new List<IMongoQuery>();
            foreach (var filter in filters)
            {
                Query.And(filter._doc);
            }
            return new MongoDBFilter(Query.And(querys));
        }

        public static MongoDBFilter Or(params MongoDBFilter[] filters)
        {
            List<IMongoQuery> querys = new List<IMongoQuery>();
            foreach (var filter in filters)
            {
                Query.Or(filter._doc);
            }
            return new MongoDBFilter(Query.Or(querys));
        }

        public static MongoDBFilter Between(string name, object startValue, object endValue)
        {
            return And(MongoDBFilter.Gte(name, startValue), MongoDBFilter.Lte(name, endValue));
        }
    }
}
